package com.it.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.it.annotation.ClearCache;
import com.it.annotation.SystemLog;
import com.it.auth.LoginInfoDTO;
import com.it.service.LoginService;
import com.it.vo.MenuVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/login")
public class LoginController {

    private final LoginService loginService;

    @ClearCache
    @PostMapping("/token")
    @SystemLog(value = "用户登录", method = "获取凭证")
    public SaResult login(@RequestBody @Validated LoginInfoDTO loginInfoDTO) {
        return loginService.login(loginInfoDTO);
    }

    @GetMapping("/routes")
    @SystemLog(value = "用户登录", method = "获取路由")
    public SaResult findAllRoutes() {
        Long loginId = StpUtil.getLoginIdAsLong();
        List<MenuVo> menus = loginService.findAllRoutes(loginId);
        return SaResult.data(menus);
    }
}